package org.dc.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;
import org.dc.R;
import org.dc.entity.Shift;
import org.dc.entity.Visit;
import org.dc.entity.WorkDay;

import java.util.ArrayList;
import java.util.List;

/**
 * @user: Vladimir Chernishov
 * @date: 2/28/12
 * @time: 10:33 PM
 */
public class FreeTimeView extends View{

    private final Context context;
    private Shift shift;
    private WorkDay workDay;
    private List<Visit> visits;
    

    public FreeTimeView(Context context) {
        super(context);
        this.context = context;
    }
    
    public void refresh(Shift shift, WorkDay workDay, List<Visit> visits){
        this.shift = shift;
        this.workDay = workDay;
        this.visits = visits;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(shift != null && workDay != null && visits != null){
            Log.d("FreeTimeView", "shift, workDay and visits are not null.");
            Paint white = new Paint();
            white.setColor(getResources().getColor(R.color.WHITE));
            canvas.drawRect(0, 0, getWidth(), 50, white);
            
            int workHours = shift.getTill() - shift.getFrom();
            int cells = workHours + 2;

            for(int i =0; i < cells; i++){
                Paint grey = new Paint();
                grey.setColor(getResources().getColor(R.color.BKG_LIGHT));
                int cellWidth = getWidth()/cells;
                int left = 3+ cellWidth * i;
                int right = cellWidth * (i +1);

                canvas.drawRect(left, 3, right, 35, grey);
                Log.d("LEFT", String.valueOf(left));
                Log.d("RIGHT", String.valueOf(right));
                
                if( i > 0 && i <= workHours ){
                    Paint black = new Paint();
                    black.setColor(getResources().getColor(R.color.BLACK));
                    String label = String.valueOf(shift.getFrom() + i - 1) +":00";
                    canvas.drawText(label , left, 20, black);
                }

                addGantCharCell(i, visits, shift, left, right, canvas);
            }

//            Paint shiftPaint = new Paint();
//            shiftPaint.setColor(getResources().getColor(R.color.BLACK));
//            for (int i = 0; i < visits.size(); i++) {
//                Visit visit = visits.get(i);
//                canvas.drawText(visit.getDescription(), 20 + 20*i, 20, shiftPaint);
//            }                                           
        } else {
            Log.d("FreeTimeView", "shift, workDay and visits are null.");
            Paint black = new Paint();
            black.setColor(getResources().getColor(R.color.WHITE));
            canvas.drawRect(0, 0, getWidth(), 5, black);
        }
    }

    private void addGantCharCell(int cellN, List<Visit> visits, Shift shift, int left, int right, Canvas canvas) {
        int hoursStart = shift.getFrom() + cellN - 1;
        List<Visit> thisHourVisits = new ArrayList<Visit>();
        for (Visit visit : visits) {
            boolean startsThisHour = visit.getTime() / 60 == hoursStart;
            boolean endsThisHour = ((visit.getTime() + visit.getLength())/60 == hoursStart) 
                    && !((visit.getTime() + visit.getLength())%60 == 0);
            boolean duringThisHour = visit.getTime()/60 < hoursStart &&
                    (visit.getTime() + visit.getLength())/60 > hoursStart;
            
            if(startsThisHour || endsThisHour || duringThisHour){
                thisHourVisits.add(visit);
            }
        }
        
        if(thisHourVisits.isEmpty()){
            drawGreen(left, right, canvas);
        } else {
            boolean colorRed = false;
            for (int i = 0; i < thisHourVisits.size(); i++) {
                Visit thisVisit = thisHourVisits.get(i);
                if(i + 1 < thisHourVisits.size()){
                    Visit nextVisit = thisHourVisits.get(i + 1);
                    boolean nextStartsBeforeThisEnds = (thisVisit.getTime() + thisVisit.getLength()) > nextVisit.getTime();
                    if(nextStartsBeforeThisEnds){
                        colorRed = true;
                    } 
                }else {
                    break;
                }
                
            }
            if(colorRed){
                drawRed(left, right, canvas);
            } else {
                drawYellow(left, right, canvas);
            }
            Paint black = new Paint();
            black.setColor(getResources().getColor(R.color.BLACK));
            String label = String.valueOf(thisHourVisits.size());
            canvas.drawText(label , left + 5, 46, black);
        }
    }

    private void drawYellow(int left, int right, Canvas canvas) {
        Paint red = new Paint();
        red.setColor(getResources().getColor(R.color.YELLOW));
        canvas.drawRect(left, 24, right, 47, red);
    }

    private void drawRed(int left, int right, Canvas canvas) {
        Paint red = new Paint();
        red.setColor(getResources().getColor(R.color.RED));
        canvas.drawRect(left, 24, right, 47, red);
    }

    private void drawGreen(int left, int right, Canvas canvas) {
        Paint green = new Paint();
        green.setColor(getResources().getColor(R.color.GREEN));
        canvas.drawRect(left, 36, right, 47, green);
    }
}
